package org.batatasframework.support.database.sql.where;

import java.util.List;

import org.batatasframework.common.NumberLimit;
import org.batatasframework.support.database.LogicSymbol;
import org.batatasframework.support.database.metadatas.FieldMetadata;
import org.batatasframework.support.database.sql.QueryParameter;
import org.batatasframework.support.database.sql.TableInstance;

import cn.bonoon.kernel.util.Opt;

/**
 * 无参数的查询条件
 * @author jackson
 *
 */
public class ZeroQueryFragmentNumberLimit extends ZeroQueryFragmentDefault{

	private final NumberLimit numberLimit;
	
	public ZeroQueryFragmentNumberLimit(TableInstance tableAlias, FieldMetadata fieldMetadata, Opt opt, NumberLimit numberLimit) {
		super(tableAlias, fieldMetadata, opt);
		
		this.numberLimit = numberLimit;
	}

	@Override
	public void parse(LogicSymbol logic, StringBuilder sql, List<QueryParameter> paramenters) throws Exception {
		sql.append(logic.getValue());
		
		if(opt == Opt.NN){
			sql.append(" not ");
		}
		
		sql.append('(').append(columnName)
			.append(" is null or ").append(columnName)
			.append("<").append(numberLimit.minValue())
			.append(" or ").append(columnName)
			.append(">").append(numberLimit.maxValue())
			.append(")");
	}

}
